/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IntervaloData.java
 *
 * Created on 31/03/2013, 18:42:53
 */
package com.bs.pesquisaView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Bone
 */
public class IntervaloData extends PesquisaAbstrata {

    private SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");

    /** Creates new form IntervaloData */
    public IntervaloData() {
        initComponents();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        dataFiltro = new com.bs.extensoesSwing.DocumentFilter();
        legenda = new javax.swing.JLabel();
        inicioField = new com.bs.extensoesSwing.CampoTexto();
        fimField = new com.bs.extensoesSwing.CampoTexto();

        dataFiltro.setRegraDeValidacaoSelecionada(com.bs.extensoesSwing.DocumentFilter.OpcoesDeRegraDeValidacao.DATA);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${legenda}"), legenda, org.jdesktop.beansbinding.BeanProperty.create("text"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);

        inicioField.setFilter(dataFiltro);

        fimField.setFilter(dataFiltro);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(legenda)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(inicioField, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fimField, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(legenda)
                .addComponent(inicioField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(fimField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.bs.extensoesSwing.DocumentFilter dataFiltro;
    private com.bs.extensoesSwing.CampoTexto fimField;
    private com.bs.extensoesSwing.CampoTexto inicioField;
    private javax.swing.JLabel legenda;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    @Override
    public Criterion getCriterion() {
        if (inicioField.getText().equals("") && fimField.getText().equals("")) {
            return null;
        }
        String msg = "Erro ao criar período.\n"
                + "Favor verifique as datas informadas.";
        if (!inicioField.getText().equals("") && fimField.getText().equals("")) {
            try {
                Date data = format.parse(inicioField.getText());
                return Restrictions.eq(getCampo(), data);
            } catch (ParseException ex) {
                Logger.getLogger(IntervaloData.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                Date dInicio = format.parse(inicioField.getText());
                Date dFim = format.parse(fimField.getText());
                return Restrictions.between(getCampo(), dInicio, dFim);
            } catch (ParseException ex) {
                Logger.getLogger(IntervaloData.class.getName()).log(Level.SEVERE, null, ex);
                
            }
        }
        return null;
    }
    public Date getDataInicial(){
        try {
            return format.parse(inicioField.getText());
        } catch (ParseException ex) {
            Logger.getLogger(IntervaloData.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    public Date getDataFinal(){
        try {
            return format.parse(fimField.getText());
        } catch (ParseException ex) {
            Logger.getLogger(IntervaloData.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
